package cn.team11.smarteye.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import cn.team11.sds.dayi.R;
import com.bumptech.glide.Glide;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.team11.smarteye.base.BaseActivity;
import cn.team11.smarteye.bean.ImageInfo;
import cn.team11.smarteye.contract.ImageDetailContract;
import cn.team11.smarteye.impl.ImageDetailPresenterImpl;
import cn.team11.smarteye.misc.SConstants;
import cn.team11.smarteye.utils.ListUtils;

/**
 * Created by Murphy on 2017/11/8.
 * 用来显示门铃图片
 */

public class ImageDetailActivity extends BaseActivity<ImageDetailContract.Presenter> implements ImageDetailContract.View, ViewPager.OnPageChangeListener{

    @BindView(R.id.image_info_pager)
    ViewPager mViewPager;
    @BindView(R.id.image_create_date)
    TextView mDateTv;
    @BindView(R.id.image_create_time)
    TextView mTimeTv;
    @BindView(R.id.image_create_loc)
    TextView mLocTv;
    @BindView(R.id.image_indicator)
    TextView mIndicator;

    private int type;
    private List<ImageInfo> mImages = new ArrayList<>();
    private ImageDetailAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_detail);
        ButterKnife.bind(this);
        init();
    }

    @Override
    public void setPresenter(ImageDetailContract.Presenter presenter) {
        this.mBasePresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showImageInfo(String date, String time, String loc) {
        if (!StringUtils.isEmpty(date))
            mDateTv.setText(getResources().getString(R.string.date) + date);
        if (!StringUtils.isEmpty(time))
            mTimeTv.setText(getResources().getString(R.string.time) + time);
        if (!StringUtils.isEmpty(loc))
            mLocTv.setText(getResources().getString(R.string.location) + loc);
    }


    @Override
    public void init() {
        mAdapter = new ImageDetailAdapter();
        mViewPager.setAdapter(mAdapter);
        mViewPager.addOnPageChangeListener(this);
        //初始化presenter
        new ImageDetailPresenterImpl(this);

        String bid = getIntent().getStringExtra(SConstants.BID);
        String fid = getIntent().getStringExtra(SConstants.FIDS);
        Long timestamp = getIntent().getLongExtra(SConstants.CREATE_TIME, 0);
        int imgtype = getIntent().getIntExtra(SConstants.IMAGE_TYPE, 0);
        if (mBasePresenter != null){
            mImages.add(mBasePresenter.createImageInfo(bid, fid, timestamp, imgtype));
            mAdapter.notifyDataSetChanged();
        }
    }



    @Override
    public void onLoginResult(boolean result) {

    }

    @Override
    public void onNetAvailable(boolean isAvailable) {

    }

    @Override
    public void onCateyeServiceBinded() {

    }


    @OnClick(R.id.navbar_leftbtn)
    public void onClickAction(View view) {
        finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (!ListUtils.isEmpty(mImages)){
            mIndicator.setText((position+1) + "/" + mImages.size());
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    public class ImageDetailAdapter extends PagerAdapter {

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = LayoutInflater.from(ImageDetailActivity.this).inflate(R.layout.vp_item_image, container, false);
            view.setTag(position);
            final ImageView image = (ImageView) view.findViewById(R.id.image_item);
            //加载图片
            //Glide.with(ImageDetailActivity.this).load(mImages.get(position).getLocation()).fitCenter().into(image);
            Glide.with(ImageDetailActivity.this).load(mImages.get(position).getUrl()).fitCenter().into(image);

            container.addView(view);
            return view;
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public int getCount() {
            return mImages.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

}
